using System;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Markup;
using Common;
using ProjectView.Windows;
using SProject;
using SProject.Paramter;
using SProject.Program;

namespace ProjectView.ProjectControls
{
	public partial class UCHRMOption : UCBaseOption, IComponentConnector
	{
		public static readonly RoutedEvent OnOptionChangedRoutedEvent = EventManager.RegisterRoutedEvent("OnOptionChangedRoutedEvent", RoutingStrategy.Bubble, typeof(EventHandler<RoutedEventArgs>), typeof(UCHRMOption));

		public static readonly DependencyProperty IsReadOnlyProperty = DependencyProperty.Register("IsReadOnly", typeof(bool), typeof(UCHRMOption), new PropertyMetadata(false));

		public static readonly DependencyProperty ProjectProperty = DependencyProperty.Register("Project", typeof(Project), typeof(UCHRMOption), new UIPropertyMetadata(null));

		public bool IsReadOnly
		{
			get
			{
				return (bool)GetValue(IsReadOnlyProperty);
			}
			set
			{
				SetValue(IsReadOnlyProperty, value);
			}
		}

		public Project Project
		{
			get
			{
				return (Project)GetValue(ProjectProperty);
			}
			set
			{
				SetValue(ProjectProperty, value);
			}
		}

		public event RoutedEventHandler OnOptionChanged
		{
			add
			{
				AddHandler(OnOptionChangedRoutedEvent, value);
			}
			remove
			{
				RemoveHandler(OnOptionChangedRoutedEvent, value);
			}
		}

		public UCHRMOption()
		{
			InitializeComponent();
			AddHandler(ButtonBase.ClickEvent, new RoutedEventHandler(OnButtonClick_RoutedEvent), true);
			grid.LoadingRow += grid_LoadingRow;
		}

		private void OnButtonClick_RoutedEvent(object sender, RoutedEventArgs e)
		{
			Button button = e.OriginalSource as Button;
			if (!IsReadOnly && button != null && button.Tag != null && !(button.Tag.ToString() != "ClassThreshold") && button.DataContext is HRMOptionItem)
			{
				HRMOptionItem hRMOptionItem = button.DataContext as HRMOptionItem;
				ClassThresholdWindow classThresholdWindow = new ClassThresholdWindow();
				classThresholdWindow.Owner = Utility.GetMainWindow();
				classThresholdWindow.ClassThreshold = hRMOptionItem.ClassThreshold;
				classThresholdWindow.ClassNum = hRMOptionItem.ClassThresholdNumList;
				if (classThresholdWindow.ShowDialog() == true)
				{
					hRMOptionItem.ClassThreshold = classThresholdWindow.ClassThreshold;
					RaiseEvent(new RoutedEventArgs
					{
						RoutedEvent = OnOptionChangedRoutedEvent,
						Source = this
					});
				}
			}
		}

		public void UnSelectAll()
		{
			grid.UnselectAllCells();
		}

		public bool Validate()
		{
			grid.EndEdit();
			bool flag = true;
			HRMOption hRMOption = base.DataContext as HRMOption;
			if (hRMOption != null && Project != null)
			{
				double num = (from s in Project.PCRProgram.Programs
					where s is MeltingSegment
					select ((MeltingSegment)s).StartTemp).FirstOrDefault();
				double num2 = (from s in Project.PCRProgram.Programs
					where s is MeltingSegment
					select ((MeltingSegment)s).EndTemp).FirstOrDefault();
				int index = -1;
				int index2 = -1;
				StringBuilder stringBuilder = new StringBuilder();
				for (int i = 0; i < hRMOption.Items.Count; i++)
				{
					HRMOptionItem hRMOptionItem = hRMOption.Items[i] as HRMOptionItem;
					index2 = i;
					string text = Utility.GetMessage("Target") + "[" + hRMOptionItem.TargetName + "]:";
					if (hRMOptionItem.StartTemperatureBefore < num)
					{
						stringBuilder.AppendFormat(Utility.GetMessage("msg_hrm_paramter_startTempbefore_invalid_over"));
						index = 2;
						flag = false;
						break;
					}
					if (hRMOptionItem.StartTemperatureBefore > hRMOptionItem.EndTemperatureBefore)
					{
						stringBuilder.AppendFormat(text + Utility.GetMessage("msg_hrm_paramter_starttempbefore_invalid_lower"));
						index = 2;
						flag = false;
						break;
					}
					if (hRMOptionItem.StartTemperatureAfter <= hRMOptionItem.EndTemperatureBefore)
					{
						stringBuilder.AppendFormat(text + Utility.GetMessage("msg_hrm_paramter_endTempbefore_invalid"));
						index = 2;
						flag = false;
						break;
					}
					if (hRMOptionItem.EndTemperatureAfter <= hRMOptionItem.StartTemperatureAfter)
					{
						stringBuilder.AppendFormat(text + Utility.GetMessage("msg_hrm_paramter_starttempafter_invalid"));
						index = 2;
						flag = false;
						break;
					}
					if (hRMOptionItem.EndTemperatureAfter > num2)
					{
						stringBuilder.AppendFormat(text + Utility.GetMessage("msg_hrm_paramter_endtempafter_invalid"));
						index = 2;
						flag = false;
						break;
					}
				}
				if (!flag && stringBuilder.Length > 0)
				{
					grid.UnselectAllCells();
					MessageBox.Show(stringBuilder.ToString());
					grid.CurrentColumn = grid.Columns[index];
					grid.CurrentItem = hRMOption.Items[index2];
					grid.BeginEdit();
				}
			}
			return flag;
		}

		private void DataGridEx_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
		{
			if (IsReadOnly)
			{
				e.Cancel = true;
			}
		}
	}
}
